/* Demo Note:  This demo uses a FileProgress class that handles the UI for displaying the file name and percent complete.
The FileProgress class is not part of SWFUpload.
 */

/* **********************
 Event Handlers
 These are my custom event handlers to make my
 web application behave the way I went when SWFUpload
 completes different tasks.  These aren't part of the SWFUpload
 package.  They are part of my application.  Without these none
 of the actions SWFUpload makes will show up in my application.
 ********************** */

function filesSwfUploadPreLoad() {
	var self = this;
	var loading = function() {
		var longLoad = function() {
		};
		this.customSettings.loadingTimeout = setTimeout(function() {
			longLoad.call(self);
		}, 15 * 1000);
	};

	this.customSettings.loadingTimeout = setTimeout(function() {
		loading.call(self);
	}, 1 * 1000);
}

function dofilesUpload(e) {

	try {
		// avatarsUploadView(false);
		swfu.startUpload();

	} catch (ex) {

	}
	return false;
}

function filesUploadLoaded() {
	var self = this;
	clearTimeout(this.customSettings.loadingTimeout);
}

function filesSwfUploadLoadFailed() {
	clearTimeout(this.customSettings.loadingTimeout);
	document.getElementById("swfBlock").style.display = "none";
	document.getElementById("ajaxBlock").style.display = "";
}

function filesFileQueued(file) {
	try {
		var txtFileName = document.getElementById("avatarsFileName");
		if (txtFileName.length > 255) {
			scmWarn(handlers.fileNameError);
		} else {
			txtFileName.value = file.name;
			document.getElementById('btnUpload').disabled = "";
		}
	} catch (ex) {
		this.debug(ex);
	}

}

function filesFileQueueError(file, errorCode, message) {
	try {
		var imageName = handlers.passError;
		switch (errorCode) {
		case SWFUpload.UPLOAD_LIMIT_EXCEEDED:
			imageName = imageName + handlers.maxNumError;
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			imageName = imageName + handlers.fileNullError;
			break;
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			imageName = imageName + handlers.sizeLimitError;
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
		default:
			break;
		}
		scmWarn(imageName);
	} catch (ex) {
		this.debug(ex);
	}
}

function filesFileDialogComplete(numFilesSelected, numFilesQueued) {
	try {

	} catch (ex) {
		this.debug(ex);
	}
}

function filesUploadStart(file) {
	try {
		var progress = new FileProgress(file,
				this.customSettings.progressTarget);
		progress.setStatus("Uploading...");
		progress.toggleCancel(false);
	} catch (ex) {
	}

	return true;
}

function filesFileDialogStart() {
	var txtFileName = document.getElementById("avatarsFileName");
	txtFileName.value = "";
	this.cancelUpload();
}

function filescancelUpload() {
	var txtFileName = document.getElementById("avatarsFileName");
	txtFileName.value = "";
}

function filesUploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
		var progress = new FileProgress(file,
				this.customSettings.progressTarget);
		progress.toggleCancel(false);
		progress.setProgress(percent);
		progress.setStatus("Uploading...");
	} catch (ex) {
		this.debug(ex);
	}
}

function filesUploadError(file, errorCode, message) {
	try {

		var txtFileName = document.getElementById("avatarsFileName");
		txtFileName.value = "";

		uploadError(file, errorCode, message, this);

	} catch (ex) {
		this.debug(ex);
	}
}

function filesUploadComplete(file) {
	var txtFileName = document.getElementById("avatarsFileName");
	txtFileName.value = "";
}

function filesQueueComplete(numFilesUploaded) {
}

function filesUploadSuccess(file, response) {
	try {
		var json = eval('(' + response + ')');
		if (json.result == 'success') {
			alert("上传成功！");
		} else {
			alert("上传失败！");
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function getCookie(key) {
	var acookie = document.cookie.split("; ");
	for ( var i = 0; i < acookie.length; i++) {
		var arr = acookie[i].split("=");
		if (key == arr[0] && arr.length > 1) {
			return unescape(arr[1]);
		}
	}
	return '';
}